package com.example.shop.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.example.shop.domain.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @projectName: travel
 * @package: com.example.travel.controller
 * @className: CaptchaController
 * @author: Liqian
 * @description: TODO
 * @date: 2024/4/21 16:49
 * @version: 1.0
 */

@Tag(name = "验证码相关", description = "这是描述")
@RestController
@RequestMapping("/captcha")
@Slf4j
public class CaptchaController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/get")
    @Operation(summary = "获取验证码")
    public Result get() {
        System.out.println("get captcha");
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 10);
        //得到验证码
        String code = lineCaptcha.getCode();
        //有效时长
        redisTemplate.expire("code",30, TimeUnit.SECONDS);
        //redis存储验证码
        redisTemplate.opsForValue().set("code", code);
        //返回Base64编码的图片JSOW字苻串
        String imageBase64Data = lineCaptcha.getImageBase64Data();
        //返回JSON格式ResponseResult.对象
        return Result.success(imageBase64Data);
    }

}
